package com.gzgy.ustore.api.dao.impl;

import com.gzgy.ustore.api.common.jdbc.StatementParameter;
import com.gzgy.ustore.api.dao.ParamTypeDao;
import com.gzgy.ustore.api.model.ParamTypeList;
import com.ustore.model.ParamType;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ParamTypeDaoImpl extends AbstractBaseDao<ParamType> implements ParamTypeDao {
    @Override
    public Integer getParamType(String paramType, String paramKey, String companyId) {

        String sql ="SELECT COUNT(ID) FROM " +this.tableName+ " WHERE " +
                "type =? AND PARAM_KEY = ? AND COMPANY_ID = ? AND IS_DELETED = 0 ";
        StatementParameter parameter = new StatementParameter();
        parameter.setString(paramType);
        parameter.setString(paramKey);
        parameter.setString(companyId);
        return this.queryForInt(sql,parameter);
    }

    @Override
    public List<ParamTypeList> paramTypeList(String companyId, String index, String page, String type, String paramKey) {
        String sql = "SELECT ID,TYPE ,PARAM_VALUE,PARAM_KEY,PARAM_KEY,PARAM_DESC FROM " +this.tableName+ " WHERE COMPANY_ID = ? ";
        StatementParameter parameter = new StatementParameter();
        parameter.setString(companyId);
        if(StringUtils.isNotEmpty(type)){
            sql = sql +" and TYPE = ?";
            parameter.setString(type);
        }
        if(StringUtils.isNotEmpty(paramKey)){
            sql = sql +" and PARAM_KEY = ?";
            parameter.setString(paramKey);
        }
        sql = sql + " AND IS_DELETED = 0 limit ?,?";
        parameter.setInt(NumberUtils.toInt(index)*NumberUtils.toInt(page));
        parameter.setInt(NumberUtils.toInt(page));
        return this.queryForList(sql,ParamTypeList.class,parameter);
    }

    @Override
    public int countParamTypeList(String companyId, String type, String paramKey) {
        StatementParameter parameter = new StatementParameter();
        parameter.setString(companyId);
        String sql ="SELECT COUNT(ID) FROM " +this.tableName+ " WHERE " +
                "  COMPANY_ID = ? ";
        if(StringUtils.isNotEmpty(type)){
            sql = sql +" and TYPE = ?";
            parameter.setString(type);
        }
        if(StringUtils.isNotEmpty(paramKey)){
            sql = sql +" and PARAM_KEY = ?";
            parameter.setString(paramKey);
        }
        sql = sql + "  AND IS_DELETED = 0 ";
        return this.queryForInt(sql,parameter);
    }
}
